﻿using IntoTheVoid.Models;
using IntoTheVoid.State;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace IntoTheVoid.Units
{
	public enum AdvertTabEnum
	{
		activity,
		notice,
		information
	};
	public class AdvertJson
	{
		public List<AdvertData> adverts { get; set; }
		public List<AdvertPictureData> pictures { get; set; }
	}
	public class AdvertData
	{
		public string title { get; set; }
		public string time { get; set; }
		public string link { get; set; }
		public AdvertTabEnum type { get; set; }
	}
	public class AdvertPictureData
	{
		public string link;
		public string pic_url;
	}
	public class ImageData
	{
		public BitmapImage bitmap;
		public string link;
	}
	public class AdvertDataManager
	{
		static string url = "https://cweb.jinzhangshu.com/launcher_tab";
		static public List<ImageData> currentListImg = new List<ImageData>();
		static public AdvertJson jsonData = new AdvertJson();
		static public event EventHandler<GameDownloadConfigEventArgs> DownLoadAdvertConfigCompleted;

		static public void GetJsonStringFromFile(string filePath)
		{
			// 测试数据
			if (filePath == null)
				return;
			if (!File.Exists(filePath))
				return;
			
			using (StreamReader fileReader = new StreamReader(filePath))
			{
				string json = fileReader.ReadToEnd();
				try
				{
					jsonData = JsonConvert.DeserializeObject<AdvertJson>(json);
				}
				catch (Exception ex)
				{
					LogHelper.Error("ReadCompareDiffListJson Error:" + ex);
				}
			}
			return;
		}


		static public void SendAdvertConfig()
		{
			Dictionary<string, object> sendDict = new Dictionary<string, object>();
			Action<AdvertJson> startConfigCallBack = StartConfigCallBack;
			HttpHelper.Instance?.HttpPostHandle(url, sendDict, onExcption: (ex) => {
				LogHelper.Error("SendAdvertConfig Error:" + ex);
			}, startConfigCallBack);
		}
		static void StartConfigCallBack(AdvertJson data)
		{
			if (data == null)
			{
				LogHelper.Error("Get AdvertJson return null" );
				return;
			}
			jsonData = data;

			if (DownLoadAdvertConfigCompleted != null)
			{
				DownLoadAdvertConfigCompleted.Invoke(null, new GameDownloadConfigEventArgs(0));
			}
		}
	}
}
